package com.loveyukari.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loveyukari.model.Auth;
import com.loveyukari.utils.RandomCodeUtil;

import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年8月7日上午10:47:59
**/
@Service
public class DrawTicketService {
	private Logger log = LoggerFactory.getLogger(DrawTicketService.class);
	
	@Autowired
	private AuthService authService;
	
	private Date startTime;
	private boolean drawTicketFlag;
	private String problem;
	private Set<Set<String>> keySet;
	private Date endTime;
	private Auth drawAuth;
	private String code;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public String start(String[] keySets,String problem){
		this.startTime = new Date();
		log.info("start draw ticket "+startTime);
		this.keySet = new HashSet<Set<String>>();
		for (int i = 0; i < keySets.length; i++) {
			String keySet = keySets[i];
			if (StringUtils.isBlank(keySet)){
				continue;
			}
			HashSet<String> tmpSet = new HashSet<String>();
			String[] split = keySet.split(",");
			for (int j = 0; j < split.length; j++) {
				String key = split[j];
				if (StringUtils.isBlank(key)){
					continue;
				}
				tmpSet.add(key);
			}
			if (tmpSet.isEmpty()){
				continue;
			}
			this.keySet.add(tmpSet);
		}
		if (this.keySet.isEmpty()){
			this.startTime = null;
			this.keySet = null;
			return "error";
		}
		this.code = RandomCodeUtil.randomUpperCode(4);
		this.problem = problem;
		this.drawTicketFlag = true;
		return sdf.format(this.startTime);
	}
	
	public void clear(){
		this.drawTicketFlag = false;
		this.startTime = null;
		this.endTime = null;
		this.keySet = null;
		this.drawAuth = null;
		this.problem = null;
		this.code = null;
	}
	
	public JSONObject getStatus(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("drawTicketFlag", this.drawTicketFlag);
		if (this.keySet != null){
			jsonObject.put("keySet", this.keySet);
		}
		if (this.problem != null){
			jsonObject.put("problem", this.problem);
		}
		if (this.startTime != null){
			jsonObject.put("startTime", sdf.format(this.startTime));
		}
		if (this.endTime != null){
			jsonObject.put("endTime", sdf.format(this.endTime));
		}
		if (this.drawAuth != null){
			JSONObject jsonAuth = new JSONObject();
			jsonAuth.put("id", this.drawAuth.getId());
			jsonAuth.put("name", this.drawAuth.getName());
			jsonAuth.put("lv", this.drawAuth.getLv());
			jsonAuth.put("fansLv", this.drawAuth.getFansLv());
			jsonObject.put("auth", jsonAuth);
		}
		return jsonObject;
	}
	
	public JSONObject getResult(){
		JSONObject result = new JSONObject();
		result.put("problem", this.problem == null ? "" : this.problem );
		result.put("code", this.code == null ? "" : this.code);
		if (this.drawAuth == null){
			result.put("auth", "");
		} else {
			JSONObject jsonAuth = new JSONObject();
			jsonAuth.put("id", this.drawAuth.getId());
			jsonAuth.put("name", this.drawAuth.getName());
			jsonAuth.put("lv", this.drawAuth.getLv());
			jsonAuth.put("fansLv", this.drawAuth.getFansLv());
			result.put("auth", jsonAuth);
		}
		return result;
	}
	
	
	public boolean isDrawChatmsg(String txt){
		if (this.drawTicketFlag && txt != null){
			String newTxt = txt.toUpperCase();
			if (StringUtils.contains(newTxt, this.code)){
				for (Set<String> set : keySet) {
					boolean flag = true;
					for (String key : set) {
						if (newTxt.contains(key.toUpperCase())){
							flag = false;
							break;
						}
					}
					if (flag){
						return false;
					}
				}
				return true;
			}
		}
		return false;
	}
	
	public void addAuth(Integer id){
		Auth auth = authService.getCacheAuth(id);
		if (auth != null && this.drawTicketFlag){
			this.drawTicketFlag = false;
			this.drawAuth = auth;
			this.endTime = new Date();
			log.info("problem:"+this.problem+";auth:"+this.drawAuth);
		}
	}

}
